package teamgodeater.hicarnet.MVP.Ui.Main;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.widget.FrameLayout;

import com.lapism.searchview.SearchAdapter;
import com.lapism.searchview.SearchItem;
import com.lapism.searchview.SearchView;
import com.rey.material.widget.FloatingActionButton;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import teamgodeater.hicarnet.Activity.ManageActivity;
import teamgodeater.hicarnet.Adapter.BaseItem2LineAdapter;
import teamgodeater.hicarnet.Layout.LoadingView.BookLoadingViewHelp;
import teamgodeater.hicarnet.Layout.LoadingView.RotateLoadingViewHelp;
import teamgodeater.hicarnet.MVP.Base.BaseFragment;
import teamgodeater.hicarnet.MVP.Base.WindowsParams;
import teamgodeater.hicarnet.MVP.Ui.Main.Adapter.BottomPagerAdapter;
import teamgodeater.hicarnet.R;
import teamgodeater.hicarnet.Utils.Utils;
import teamgodeater.hicarnet.Widget.RippleBackGroundView;

import static teamgodeater.hicarnet.C.VT_MAIN;

/**
 * Created by G on 2016/6/18 0018.
 */

public class MainFragment extends BaseFragment<MainPresent> implements MainContractor.View {
    @Bind(R.id.zoomLocButton)
    RippleBackGroundView zoomLocButton;
    @Bind(R.id.bottomViewPager)
    ViewPager bottomViewPager;
    @Bind(R.id.searchView)
    SearchView searchView;
    @Bind(R.id.viewPagerFramelayout)
    FrameLayout viewPagerFramelayout;
    @Bind(R.id.pagerSelect1)
    View pagerSelect1;
    @Bind(R.id.pagerSelect2)
    View pagerSelect2;
    @Bind(R.id.detail_return)
    FloatingActionButton detailReturn;
    @Bind(R.id.detail_buy)
    FloatingActionButton detailBuy;
    @Bind(R.id.detail_go)
    FloatingActionButton detailGo;

    SearchAdapter searchAdapter;
    @Bind(R.id.detail_top)
    CardView detailTop;
    @Bind(R.id.detail_top_recycleView)
    RecyclerView detailTopRecycleView;
    @Bind(R.id.selectGradient)
    View selectGradient;
    @Bind(R.id.actionFab)
    FloatingActionButton actionFab;

    private BottomPagerAdapter bottomPagerAdapter;
    private RotateLoadingViewHelp rotateLoading;
    private BookLoadingViewHelp bottomRotateLoading;

    public static MainFragment getInstans() {
        List<Fragment> fragments = ManageActivity.manageActivity.getSupportFragmentManager().getFragments();
        if (fragments != null && fragments.size() != 0)
            for (Fragment f : fragments) {
                if (f instanceof MainFragment) {
                    return (MainFragment) f;
                }
            }
        return new MainFragment();
    }

    @NonNull
    @Override
    protected WindowsParams onCreateSupportViewParams(WindowsParams params) {
        params.rootLayoutId = R.layout.frgm_main_main;
        params.isHasToolBar = false;
        params.isNoFullScreen = false;
        return params;
    }

    @Override
    public String getType() {
        return VT_MAIN;
    }

    public void mapFirstShow() {
        mPresenter.mapPresent.mapFristShow();
        mPresenter.gasStationPresent.showBefor();
        mPresenter.overViewPresent.addToPager();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, mRootContain);
        setColorFilter();
        setView();
        setListener();
        return rootView;
    }

    @Override
    public void onOnceGlobalLayoutListen() {
        fixViewSize();
        int margin = Utils.dp2Px(162);
        int topMargin = manageActivity.getStatusBarHeight() + margin;
        detailTop.setTranslationY(-detailTop.getHeight() - topMargin);
        hideOrderFab();
    }

    private void fixViewSize() {
        ViewGroup.LayoutParams layoutParams = bottomViewPager.getLayoutParams();
        int width = viewPagerFramelayout.getWidth();
        layoutParams.width = width;
        int height = viewPagerFramelayout.getHeight();
        layoutParams.height = height;
        bottomViewPager.setLayoutParams(layoutParams);

        layoutParams = detailTopRecycleView.getLayoutParams();
        layoutParams.width = width;
        detailTopRecycleView.setLayoutParams(layoutParams);

        bottomRotateLoading.fixSize(width, height);
    }

    public void showActionFab(int drawableId) {
        Drawable drawable = getResources().getDrawable(drawableId);
        drawable.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
        actionFab.setIcon(drawable, false);
        actionFab.setVisibility(View.VISIBLE);
        actionFab.setScaleX(0.001f);
        actionFab.setScaleY(0.001f);
        actionFab.animate().scaleX(1f).scaleY(1f).setListener(null).start();
    }

    public void hideActionFab() {
        actionFab.animate().scaleX(0.001f).scaleY(0.001f).setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                actionFab.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                super.onAnimationCancel(animation);
                actionFab.setVisibility(View.GONE);
            }
        }).start();
    }

    @Override
    public void onResume() {
        super.onResume();
        manageActivity.onMapResume();
    }

    private void setView() {
        rotateLoading = new RotateLoadingViewHelp(mTopView);
        bottomRotateLoading = new BookLoadingViewHelp(viewPagerFramelayout);

        rotateLoading.setLoadingBg(true);
        bottomRotateLoading.setLoadingBg(true, Color.WHITE);
        bottomRotateLoading.setTextBackGroundColor(Utils.getColorFromRes(R.color.colorPrimary));

        bottomPagerAdapter = new BottomPagerAdapter(getContext());
        bottomViewPager.setAdapter(bottomPagerAdapter);

        int margin = Utils.dp2Px(16);
        int topMargin = manageActivity.getStatusBarHeight() + margin / 2;
        searchView.setVersionMargins(topMargin, margin, margin / 2);
        searchAdapter = new SearchAdapter(Utils.getContext());
        searchView.setAdapter(searchAdapter);
        detailTopRecycleView.setHasFixedSize(true);
        detailTopRecycleView.setLayoutManager(new LinearLayoutManager(getContext()));

        hideActionFab();
    }


    private void setListener() {
        searchAdapter.setOnItemClickListener(new SearchAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(SearchItem item) {
                mPresenter.mapSearchPresent.showRoute(item);
            }
        });
        searchView.setOnVoiceClickListener(new SearchView.OnVoiceClickListener() {
            @Override
            public void onVoiceClick() {
                mPresenter.speechPresent.openDetail();
            }
        });
        searchView.setOnMenuClickListener(new SearchView.OnMenuClickListener() {
            @Override
            public void onMenuClick() {
                manageActivity.openDrawer();
            }
        });
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextChange(String newText) {
                mPresenter.mapSearchPresent.searchLocation(newText);
                return true;
            }

            @Override
            public boolean onQueryTextSubmit(String query) {
                mPresenter.mapSearchPresent.searchLocation(query);
                return true;
            }
        });

        searchView.setOnOpenCloseListener(new SearchView.OnOpenCloseListener() {
            @Override
            public void onClose() {
                mPresenter.trafficPointPresent.setgMarkerVisible(true);
            }

            @Override
            public void onOpen() {
                mPresenter.trafficPointPresent.setgMarkerVisible(false);
            }
        });
        searchView.setOnBackClickListener(new SearchView.OnBackClickListener() {
            @Override
            public void onBackClick() {
                backClick();
            }
        });

        bottomViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position)
            {
                changePagerSelect(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        zoomLocButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.mapPresent.zoomToFit();
            }
        });

        detailGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.gasStationPresent.orderGoClick();
            }
        });

        detailReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.gasStationPresent.orderReturnClick();
            }
        });

        detailBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.gasStationPresent.orderBuyClick();
            }
        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden) {
            mPresenter.overViewPresent.addToPager();
            if (searchView.getText().equals(""))
                mPresenter.gasStationPresent.showBefor();
        }
    }

    private void backClick() {
        hidePagerLoading();
        mPresenter.trafficPointPresent.reset();
        mPresenter.mapSearchPresent.removeResult();
        mPresenter.mapPresent.setMyLocVisible(true);
        mPresenter.gasStationPresent.showBefor();
        mPresenter.mapPresent.zoomToFit();
    }

    private void setColorFilter() {
        changePagerSelect(0);
        zoomLocButton.getDrawable().mutate().setColorFilter(getResources().getColor(R.color.colorBlack54), PorterDuff.Mode.SRC_IN);
        detailBuy.getIcon().mutate().setColorFilter(getResources().getColor(R.color.colorBlack54), PorterDuff.Mode.SRC_IN);
        detailGo.getIcon().mutate().setColorFilter(getResources().getColor(R.color.colorBlack54), PorterDuff.Mode.SRC_IN);
        detailReturn.getIcon().mutate().setColorFilter(getResources().getColor(R.color.colorBlack54), PorterDuff.Mode.SRC_IN);
    }

    private void changePagerSelect(int position) {
        if (position == 0) {
            pagerSelect1.setAlpha(0.87f);
            pagerSelect2.setAlpha(0.34f);
        } else {
            pagerSelect1.setAlpha(0.34f);
            pagerSelect2.setAlpha(0.87f);
        }
    }

    public void viewPagerAnimationVisible(final boolean visible, final Runnable runnable) {
        int height = viewPagerFramelayout.getHeight();
        viewPagerFramelayout.animate().translationY(visible ? 0f : height)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationCancel(Animator animation) {
                        super.onAnimationCancel(animation);
                        if (runnable != null)
                            runnable.run();
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        if (runnable != null)
                            runnable.run();
                        if (visible) {
                            selectGradient.setVisibility(View.VISIBLE);
                            pagerSelect1.setVisibility(View.VISIBLE);
                            pagerSelect2.setVisibility(View.VISIBLE);
                        } else {
                            selectGradient.setVisibility(View.GONE);
                            pagerSelect1.setVisibility(View.GONE);
                            pagerSelect2.setVisibility(View.GONE);
                        }
                    }
                }).setInterpolator(new AccelerateInterpolator()).setDuration((long) (height * 0.5)).start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @Override
    public void showOrderFab() {
        detailBuy.setClickable(true);
        detailReturn.setClickable(true);
        detailGo.setClickable(true);
        detailBuy.setVisibility(View.VISIBLE);
        detailReturn.setVisibility(View.VISIBLE);
        detailGo.setVisibility(View.VISIBLE);
        detailBuy.setScaleX(0.0001f);
        detailGo.setScaleX(0.0001f);
        detailReturn.setScaleX(0.0001f);
        detailBuy.setScaleY(0.0001f);
        detailGo.setScaleY(0.0001f);
        detailReturn.setScaleY(0.0001f);
        detailBuy.animate().scaleY(1f).scaleX(1f).setInterpolator(new AccelerateInterpolator()).start();
        detailReturn.animate().scaleY(1f).scaleX(1f).setInterpolator(new AccelerateInterpolator()).setStartDelay(300).start();
        detailGo.animate().scaleY(1f).scaleX(1f).setInterpolator(new AccelerateInterpolator()).setStartDelay(300).start();
    }

    @Override
    public void hideOrderFab() {
        detailBuy.setClickable(false);
        detailReturn.setClickable(false);
        detailGo.setClickable(false);
        detailBuy.setVisibility(View.INVISIBLE);
        detailReturn.setVisibility(View.INVISIBLE);
        detailGo.setVisibility(View.INVISIBLE);
    }

    @Override
    public void setOrderTopAdapter(BaseItem2LineAdapter adapter) {
        detailTopRecycleView.setAdapter(adapter);
    }

    @Override
    public void showOrderTop() {
        searchView.setClickable(false);

        int margin = Utils.dp2Px(16);
        int topMargin = manageActivity.getStatusBarHeight() + margin / 2;

        detailTop.animate().translationY(topMargin).setStartDelay(900L).setInterpolator(new AccelerateInterpolator()).start();
        searchView.animate().alpha(0f).setInterpolator(new AccelerateInterpolator()).start();
    }

    @Override
    public void hideOrderTop() {
        searchView.setClickable(true);

        searchView.setAlpha(0f);

        int margin = Utils.dp2Px(16);
        int topMargin = manageActivity.getStatusBarHeight() + margin / 2;

        detailTop.animate().translationY(-detailTop.getHeight() - topMargin).start();
        searchView.animate().alpha(1f).start();
    }

    @Override
    public void openSearch() {
        searchView.open(true);
    }

    @Override
    public void closeSearch() {
        searchView.close();
    }

    @Override
    public void setSearchText(String text) {
        searchView.setText(text);
    }

    @Override
    public void setMusic(Bitmap fenmian, String musicName, String artistName, String allTime) {

    }

    @Override
    public void setMusicCurrentTime(String time, int percent) {

    }

    @Override
    public void setMusicPlay(boolean play) {

    }

    @Override
    public void changeViewPagerIndex(int index) {
        bottomViewPager.setCurrentItem(index, true);
    }

    @Override
    public void setFirstViewPager(RecyclerView.Adapter v) {
        bottomPagerAdapter.rv1.setAdapter(v);
    }

    @Override
    public void setSecondViewPager(RecyclerView.Adapter v) {
        bottomPagerAdapter.rv2.setAdapter(v);
    }

    @Override
    public void showLoading(String tip) {
        rotateLoading.startAnimator(tip);
    }

    @Override
    public void showPagerLoading(String tip) {
        bottomRotateLoading.startAnimator(tip);
    }

    @Override
    public void hideLoading() {
        rotateLoading.stopAnimator();
    }

    @Override
    public void hidePagerLoading() {
        bottomRotateLoading.stopAnimator();
    }

    @Override
    public boolean onInterceptBack() {
        if (searchView.isSearchOpen()) {
            searchView.close();
            return true;
        }
        if (!searchView.isSearchOpen() && !searchView.getText().equals("")) {
            searchView.close();
            backClick();
            return true;
        }
        if (mPresenter.gasStationPresent.marketVisible && bottomViewPager.getCurrentItem() == 1) {
            mPresenter.gasStationPresent.showShortGasStation();
            mPresenter.mapPresent.zoomToFit();
            return true;
        }
        if (detailReturn.getVisibility() == View.VISIBLE) {
            mPresenter.gasStationPresent.orderReturnClick();
            return true;
        }

        return false;
    }
}
